package com.jzo2o.customer.service.impl;/**
 * @author wl
 * @version 1.0
 * @since 2025-09-17 16:31
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.dto.request.CertificationAuditReqDTO;
import com.jzo2o.customer.service.IAgencyCertificationAuditService;
import com.jzo2o.customer.service.IAgencyCertificationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 *des:
 *data Created in 2025-09-17 16-31
 */
@Service
public class AgencyCertificationAuditServiceImpl extends ServiceImpl<AgencyCertificationAuditMapper, AgencyCertificationAudit> implements IAgencyCertificationAuditService {

    @Resource
    private IAgencyCertificationService iAgencyCertificationService;



    @Override
    public void insertAudit(AgencyCertificationAudit agencyCertificationAudit) {

        save(agencyCertificationAudit);
    }

    /**
     * 机构审核
     */
    @Override
    public void AduitAgencyCertificationAudit(Long id, CertificationAuditReqDTO certificationAuditReqDTO) {

        //获取当前登录人id
        Long userId = UserContext.currentUserId();
        //1.认证通过
        if (certificationAuditReqDTO.getCertificationStatus() == 2){
            //修改信息表
            //根据id修改信息表状态
            Long serveProviderId = getById(id).getServeProviderId();
           AgencyCertification agencyCertification = iAgencyCertificationService.getById(serveProviderId);
            agencyCertification.setCertificationStatus(2);
            agencyCertification.setId(serveProviderId);
            iAgencyCertificationService.updateById(agencyCertification);

            //修改认证表
            AgencyCertificationAudit agencyCertificationAudit = getById(id);
            agencyCertificationAudit.setId(id);
            agencyCertificationAudit.setAuditStatus(1);
            agencyCertificationAudit.setCertificationStatus(2);
            agencyCertificationAudit.setUpdateTime(LocalDateTime.now());
            agencyCertificationAudit.setAuditorId(userId);
            //根据userId查询当前人姓名
            agencyCertificationAudit.setAuditorName(UserContext.currentUser().getName());
            agencyCertificationAudit.setAuditTime(LocalDateTime.now());
            this.updateById(agencyCertificationAudit);
        }else {
            //2.认证失败
            //修改信息表
            Long serveProviderId = getById(id).getServeProviderId();
            AgencyCertification agencyCertification = iAgencyCertificationService.getById(serveProviderId);
            agencyCertification.setCertificationStatus(3);
            agencyCertification.setId(serveProviderId);
            iAgencyCertificationService.updateById(agencyCertification);
            //修改认证表
            AgencyCertificationAudit agencyCertificationAudit = getById(id);
            agencyCertificationAudit.setId(id);
            agencyCertificationAudit.setAuditStatus(1);
            agencyCertificationAudit.setCertificationStatus(3);
            agencyCertificationAudit.setUpdateTime(LocalDateTime.now());
            agencyCertificationAudit.setAuditorId(userId);
            agencyCertificationAudit.setAuditorName(UserContext.currentUser().getName());
            agencyCertificationAudit.setUpdateTime(LocalDateTime.now());
            agencyCertificationAudit.setRejectReason(certificationAuditReqDTO.getRejectReason());
            updateById(agencyCertificationAudit);
        }
    }

    /**
     * 获取机构审核拒绝原因
     */
    @Override
    public String rejectReason(Long userId) {
        LambdaQueryWrapper<AgencyCertificationAudit> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AgencyCertificationAudit::getServeProviderId, userId)
                .eq(AgencyCertificationAudit::getAuditStatus, 1)
                .eq(AgencyCertificationAudit::getCertificationStatus, 3);
        AgencyCertificationAudit one = getOne(queryWrapper);
        return one.getRejectReason();
    }
}
